﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Shop.Data;

namespace Shop.CartResources
{
    public partial class CreateCartItemForm : Form
    {
        private Data.ProductType productType;
        private ShopItem innerShopItem;
        public ICartItem ConvertedCart { get; set; }
        private IEnumerable<ICartItem> cartItems;

        public CreateCartItemForm(string shopItemBarcode,IEnumerable<ICartItem> cartItems)
        {
            InitializeComponent();
            ShopItem givenShopItem = Shop.Data.ShopData.FindItemByBarcode(shopItemBarcode);
            this.innerShopItem = givenShopItem;

            this.radTextBoxBarcode.Text = givenShopItem.Barcode;
            this.radTextBoxSpecification.Text = givenShopItem.Specification;
            this.radTextBoxPrice.Text = givenShopItem.Price.ToString();

            if (givenShopItem.Quantity != null)
            {
                this.productType = ProductType.Quantity;
                this.radLabelQuantityOrWeight.Text = "Брой продукти";
            }
            else
            {
                this.productType = ProductType.Weight;
                this.radLabelQuantityOrWeight.Text = "Покупка на КГ";
            }
            this.cartItems = cartItems;
        }

        private void radButtonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void radButtonAdd_Click(object sender, EventArgs e)
        {
            CreateCartItem();
        }
  
        private void CreateCartItem()
        {
            ICartItem cartItem = default(ICartItem);
            bool wrongInput = false;
            if (productType == ProductType.Quantity)
            {
                try
                {
                    CheckProductForAvailability();
                    CreateQuantityProduct(ref cartItem);
                }
                catch (FormatException)
                {
                    wrongInput = true;
                    MessageBox.Show("Грешно въведен брой!");
                }
                catch (InvalidOperationException exception)
                {
                    wrongInput = true;
                    MessageBox.Show(exception.Message);
                }
            }
            else if (productType == ProductType.Weight)
            {
                try
                {
                    CheckProductForAvailability();
                    CreateWeightProduct(ref cartItem);
                }
                catch (FormatException)
                {
                    wrongInput = true;
                    MessageBox.Show("Грешно въведени КГ");
                }
                catch (InvalidOperationException exception)
                {
                    wrongInput = true;
                    MessageBox.Show(exception.Message);
                }
            }
            if (!wrongInput)
            {
                this.Close();
            }
        }
  
        private ICartItem CreateWeightProduct(ref ICartItem cartItem)
        {
            decimal? weightToBuy = decimal.Parse(this.radTextBoxQuantityOrWeight.Text);
            cartItem = new WeightItem(innerShopItem, weightToBuy);
            ConvertedCart = cartItem;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            return cartItem;
        }
  
        private ICartItem CreateQuantityProduct(ref ICartItem cartItem)
        {
            int quantityToBuy = int.Parse(this.radTextBoxQuantityOrWeight.Text);
            cartItem = new QuantityItem(innerShopItem, quantityToBuy);
            ConvertedCart = cartItem;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            return cartItem;
        }

        public void CheckProductForAvailability()
        {
            if (productType == ProductType.Quantity)
            {
                CheckQuantityProductAvailability();
            }
            else
            {
                CheckWeightItemAvailability();
            }
        }
  
        private void CheckWeightItemAvailability()
        {
            decimal? weightToBuy = decimal.Parse(this.radTextBoxQuantityOrWeight.Text);
            if (this.innerShopItem.Weight < weightToBuy)
            {
                throw new InvalidOperationException("Въведете стойност по-малка от " + this.innerShopItem.Weight);
            }

            decimal? weightAlreadyBought = 0;
            foreach (var currentItem in this.cartItems)
            {
                WeightItem currentQuantityItem = currentItem as WeightItem;
                weightAlreadyBought += currentQuantityItem.WeightBought;
            }
            if (innerShopItem.Weight < weightToBuy + weightAlreadyBought)
            {
                throw new InvalidOperationException("Въведете КГ по-малки от " + (innerShopItem.Weight - weightAlreadyBought));
            }
        }
  
        private void CheckQuantityProductAvailability()
        {
            int quantityToBuy = int.Parse(this.radTextBoxQuantityOrWeight.Text);
            if (this.innerShopItem.Quantity < quantityToBuy)
            {
                throw new InvalidOperationException("Въведете стойност по-малка от " + this.innerShopItem.Quantity);
            }

            int quantityAlreadyBought = 0;
            foreach (var currentItem in this.cartItems)
            {
                QuantityItem currentQuantityItem = currentItem as QuantityItem;
                quantityAlreadyBought += currentQuantityItem.QuantityBought;
            }
            if (innerShopItem.Quantity < quantityToBuy + quantityAlreadyBought)
            {
                throw new InvalidOperationException("Въведете брой по-малък от " + (innerShopItem.Quantity - quantityAlreadyBought));
            }
        }
    }
}
